package com.pomr.controller.equipmentstorage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.pomr.entity.StorageAllocation;
import com.pomr.model.equipmentstorage.AllocationMessage;
import com.pomr.model.menu.EquipmentKind;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pomr.model.equipmentstorage.EquipmentInStorage;
import com.pomr.model.equipmentstorage.EquipmentStorage;
import com.pomr.model.equipmentstorage.EquipmentStorageDetail;
import com.pomr.model.sys.User;
import com.pomr.service.equipmentstorage.StorageService;
import com.pomr.util.common.JWT;
import com.pomr.util.common.ResponseData;
import com.pomr.util.page.Page;
import com.pomr.util.page.PageParam;
/**
 * 设备库存
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/storage")
public class StorageController {
	@Autowired
	@Qualifier("storageServiceImpl")
	private StorageService storageService;
	
	/**
	 * 查询库存信息  （厂商、机构、设备）
	 * @return
	 */
	@RequestMapping(value="/inter-search",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<EquipmentStorage>> searchEquipmentInStorages(PageParam param){
		try{
			String page = param.getPage();
			String limit = param.getLimit();
			Map<String, String> map = param.getParams();
			String equipmentid = map.get("equipmentid");
			String agencyid = map.get("agencyid");
			String manufacturerid = map.get("manufacturerid");
			return storageService.searchStorages(equipmentid, agencyid, manufacturerid, page, limit);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 通过主表id查询库存详细信息 
	 * @return
	 */
	@RequestMapping(value="/inter-searchdetail",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<EquipmentStorageDetail>> searchStorages(PageParam param){
		try{
			
			String page = param.getPage();
			String limit = param.getLimit();
			String id = param.getParams().get("mid");
			return storageService.searchStorageDetails(id, page, limit);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	/**
	 * 删除设备
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/inter-delete",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteEquipmentStorages(@RequestBody Map<String,String> map){
		ResponseData pd = null;
		try {
			String ids = map.get("id");
			if(ids==null || ids.equals("")){
				pd = ResponseData.custom("参数错误，未传递主键");
			}else{
				if(storageService.deleteStorageDetails(ids.split(","))){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.custom("删除失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		return pd;
	}
	
	/**
	 * 设备报损
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/inter-damage",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData damageSimStorages(@RequestBody Map<String,String> map){
		ResponseData pd = null;
		try {
			String ids = map.get("id");
			if(ids==null || ids.equals("")){
				pd = ResponseData.custom("参数错误，未传递主键");
			}else{
				if(storageService.damageStorageDetails(ids.split(","))){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.custom("报损失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		return pd;
	}
	
	/**
	 * 设备清库
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/inter-clean",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData cleanEquipStorages(@RequestBody Map<String,String> map){
		ResponseData pd = null;
		try {
			String ids = map.get("id");
			if(ids==null || ids.equals("")){
				pd = ResponseData.custom("参数错误，未传递主键");
			}else{
				String reason = map.get("reason");
				if(storageService.cleanStorageDetails(ids.split(","),reason)){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.custom("清库失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		return pd;
	}
	
	/**
	 * 设备修复
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/inter-repair",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData repairSimStorages(@RequestBody Map<String,String> map){
		ResponseData pd = null;
		try {
			String ids = map.get("id");
			if(ids==null || ids.equals("")){
				pd = ResponseData.custom("参数错误，未传递主键");
			}else{
				if(storageService.repairStorageDetails(ids.split(","))){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.custom("报损失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		return pd;
	}
	
	/**
	 * 更新领用定金
	 * @param storage
	 * @return
	 */
	@RequestMapping(value="/inter-deitmoney",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData editEarnestmoney(@RequestBody EquipmentStorage storage){
		ResponseData pd = null;
		try {
			if(storage==null || storage.getId()==null){
				pd = ResponseData.custom("参数错误，未传递主键");
			}else{
				if(storageService.editMoney(storage)){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.custom("更新失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		return pd;
	}
	
	/**
	 * 通过sn编码批量调拨库存
	 * @return
	 */
	@RequestMapping(value="/inter-allocations",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData allocationStorages(@RequestBody Map<String,String> map, HttpServletRequest request){
		ResponseData pd = null;
		try {
			String id = map.get("id");
			String sn1 = map.get("sn1");
			String sn2 = map.get("sn2");
			String agid = map.get("agid");
			if(id==null || "".equals(id)){
				pd = ResponseData.custom("未上传库存信息");
			}else if((sn1==null || "".equals(sn1))&&(sn2==null || "".equals(sn2))){
				pd = ResponseData.custom("sn编码未上传");
			}else if(agid==null || "".equals(agid)){
				pd = ResponseData.custom("未上传目标库存机构");
			}else{
				User user = JWT.findUserByToken(request);
				int num = storageService.allocationStorage(id, sn1, sn2, agid,user);
				pd = ResponseData.ok();
				pd.putDataValue("num", num);
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		return pd;
	}
	
	/**
	 * 通过sn模糊匹配设备
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/inter-searchBySn",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<EquipmentStorageDetail>> findEquipmentsBySn(PageParam param, HttpServletRequest request) throws Exception{
		User user  = JWT.findUserByToken(request);
		return storageService.searchEquipmentsBySn(param.getPage(),param.getLimit(), param.getParams().get("conds"),user);
	}

	/**
	 * 设备报修
	 * @param reason 报修原因
	 * @param sn 设备编码
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/app-repair",method=RequestMethod.GET)
	@ResponseBody
	public ResponseData repairOfEquipment(String sn, String reason) throws Exception{
		ResponseData pd = null;

		if(sn==null || sn.equals("")){
			pd = ResponseData.custom("请上传设备SN编码");
		}else{
			return storageService.repairEquip(sn, reason);
		}

		return pd;
	}

	/**
	 *查询机构库存设备列表
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/app-searchEquipments",method=RequestMethod.GET)
	@ResponseBody
	public ResponseData searchEquipmentOfAgency(String agencyid) throws Exception{
		ResponseData pd = null;
		if(agencyid==null || "".equals(agencyid)){
			pd = ResponseData.custom("请上传机构编号");
		}else{
			List<EquipmentKind> kids = storageService.searchEquipmentByAgency(agencyid);
			pd = ResponseData.ok();
			pd.putDataValue("equipments",kids);
		}
		return pd;
	}

	/**
	 * 获取设备详情
	 * @param storageId 库存主键
	 * @param sn1 开始sn
	 * @param sn2 结束sn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/inter-seachDetailsBySn",method=RequestMethod.GET)
	@ResponseBody
	public ResponseData seachDetailsBySn(String storageId, String sn1, String sn2) throws Exception{
		return storageService.seachDetailsBySn(storageId,  sn1,  sn2);
	}

	/**
	 * 库存调拨
	 * @param storageAllocation 调拨参数
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/inter-storageAllocation",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData storageAllocation(@RequestBody StorageAllocation storageAllocation, HttpServletRequest request) throws Exception{
		System.out.println(storageAllocation.toString());
		return storageService.storageAllocation(storageAllocation, JWT.findUserByToken(request));
	}

	@RequestMapping(value="/inter-searchAllocations",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<AllocationMessage>> findAllocationMessages(PageParam param, HttpServletRequest request) throws Exception{
		User user  = JWT.findUserByToken(request);
		return storageService.findAllocationMessages(param, user);
	}
	@RequestMapping(value="/inter-searchAllocationDetail",method=RequestMethod.GET)
	@ResponseBody
	public ResponseData seachAllocationMessageDetails(String aid)throws Exception{
		return storageService.seachAllocationMessageDetails(aid);
	}
}
